package com.example.goods_service.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.example.goods_service.config.DiGuiConfig;
import com.example.goods_service.config.OSSClientUtil;
import com.example.goods_service.return_data.Mall_Data;
import com.example.goods_service.service.GoodsService_chenbin;
import com.example.goods_service.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.InflaterInputStream;

@RestController
@Api
public class GoodsController_chenbin
{
      @Autowired
      GoodsService_chenbin goodsService_chenbin;
      @Autowired
      Mall_Data mall_data =new Mall_Data();
      //图片
      String goodspicture ;
      List<T_commodity> good;
      Double goodsrice;

      @Autowired
      OSSClientUtil ossClientUtil;

      @ApiOperation("查询商品类目")
      @RequestMapping("/selgoodsleimu")
      public Mall_Data selgoodsleimu()
      {
          List<T_commodity_category> big =  goodsService_chenbin.sel_big_leimu();
          List<T_commodity_category> small = goodsService_chenbin.sel_small_leimu();
          DiGuiConfig.getleimu(big,small);
          return mall_data.setdata(big);
      }

      @ApiOperation("查询类目属性")
      @ApiImplicitParam(name = "category_id",value = "类目id")
      @RequestMapping("/selgoodsattribute")
      public Mall_Data selgoodsattribute(@RequestParam("category_id")int category_id)
      {     //查询商品属性
            List<T_category_attribute> a = goodsService_chenbin.sel_attribute(category_id);
            //查询属性详情
            List<T_attribute_details> details = goodsService_chenbin.sel_details();
            DiGuiConfig.getdetails(a,details);
            return mall_data.setdata(a);
      }

      @ApiOperation("查询类目配置")
      @ApiImplicitParam(name = "category_id",value = "类目id")
      @RequestMapping("/selgoodconfig")
      public Mall_Data selgoodsconfig(@RequestParam("category_id")int category_id)
      {

            //查询类目配置
            List<T_category_config> config = goodsService_chenbin.sel_goodsconfig(category_id);
            System.out.println("查询商品类目的配置");

            List<T_config_details> details = goodsService_chenbin.sel_configdetails();
            DiGuiConfig.getconfig(config,details);

            return mall_data.setdata(config);
      }

     /* @ApiOperation("添加商品图片")
      @ApiImplicitParam(name = "image",value = "图片")
      @GetMapping("/addpicture")*/
      public String addpicture(@RequestParam  List<MultipartFile> image)  {
            //接收前端传过来的图片集合，上传到阿里云oss仓库保存图片
            ossClientUtil.checkList(image);
            //分割下载的图片地址  ossClientUtil.checkList(image)得到图片所有url  是以，分开的
            String[] strArr = ossClientUtil.checkList(image).split("\\,");

            //遍历url
            for (int i = 0; i < strArr.length-1; ++i){
                  goodspicture = strArr[0];
                  System.out.println(strArr[i]);
                  //添加到数据库图片表
                int  n = goodsService_chenbin.add_picture(strArr[i]);
            }
            return "成功";
      }

      @ApiOperation("添加商品")
      @ApiImplicitParams({
              @ApiImplicitParam(name = "goodsname",value = "商品名称"),
              @ApiImplicitParam(name = "goodsjianjie",value = "商品详情"),
              @ApiImplicitParam(name = "leimuid",value = "类目id"),
              @ApiImplicitParam(name = "shuxinid",value = "属性id集合"),
              @ApiImplicitParam(name = "allconfig",value = "所选配置集合"),
              @ApiImplicitParam(name = "allimage",value = "图片集合")
      })
      @RequestMapping("/addgoods")
      public Mall_Data addgoods(@RequestParam(value = "goodsname",required = false)String goodsname,
                                @RequestParam(value = "goodsjianjie",required = false)String goodsjianjie,
                                @RequestParam(value = "leimuid",required = false)int leimuid,
                                @RequestParam(value = "allshuxin",required = false)String allshuxin,
                                @RequestParam(value = "allconfig",required = false)String allconfig,
                                @RequestParam("allimage") MultipartFile[] allimage,
                                HttpServletRequest request
                                )
      {

             //添加商品表
            int n = goodsService_chenbin.add_goods(goodsname,goodsjianjie,leimuid);

            good = goodsService_chenbin.sel_goodsid(goodsname);

            //上传图片到oss   并且添加到图片表
            List<MultipartFile> filelist = new ArrayList<>();
            for (MultipartFile file : allimage) {
                  filelist.add(file);
            }
            addpicture(filelist);

            System.out.println("第一张照片--"+goodspicture);
            goodsService_chenbin.add_goods_pic(goodspicture,good.get(0).getId());

            //属性详情表数据拆分
            String[] shuxinselect =  allshuxin.split("/");
            for (String s : shuxinselect) {
                  System.out.println("属性详情---"+s);
                  String[] attribute_detailsid = s.split(":");
                  int attribute_id = Integer.parseInt(attribute_detailsid[0]);
                  int attribute_details_id = Integer.parseInt(attribute_detailsid[1]);
                  System.out.println(attribute_id+"==="+attribute_details_id);
                  //添加数据到t_selected_attribute
                  goodsService_chenbin.add_selectedattribute(attribute_id,attribute_details_id,good.get(0).getId());
            }



            //配置表数据拆分
            String t = allconfig;
            //分割传过来的字符串
            String[] split = t.split("/");
            List<List> z = new ArrayList<>();
            for (String s : split) {//遍历截取完的一整条，一整条字符串
                  List<String> z1 = new ArrayList<>();
                  for (String s1 : s.split("-")) {//一整个字符又切割成一个一个的小字符
                         z1.add(s1);//将小字符add到z1
                  }
                  z.add(z1);//将小字符的集合add到z中，z就是装一整条配置
            }
            for (int i = 0; i < z.size(); i++) {//根据一条一条去遍历
                  if (i == 0) {

                  } else {
                        //判断子集合的元素个数之差
                        int max = (z.get(i).size() - z.get(i - 1).size()) * -1;
                        if (max != 0) {//就说明有空缺的值
                              for (int i1 = 0; i1 < max; i1++) {
                                    z.get(i).add(i1,z.get(i-1).get(i1));
                              }
                        }//否则两子集相等
                  }
            }

            z.forEach(s->{
                  String configattibute = "";
                   goodsrice = Double.parseDouble((String) s.get(s.size()-2));
                  int stock = Integer.parseInt((String) s.get(s.size()-1));
                  for (int i = 0; i < s.size()-2; i++) {
                        configattibute += s.get(i);
                        //去查询配置详情的id 添加到t_selected_config表
                        System.out.println("遍历配置集合---》"+s.get(i));
                        List<T_config_details> config = goodsService_chenbin.sel_configdetailsid(String.valueOf(s.get(i)));
                        System.out.println("查出配置id---"+config.get(0).getId());
                        int a = goodsService_chenbin.add_selected_config(config.get(0).getId(),config.get(0).getConfig_id(),good.get(0).getId());
                  }

                  System.out.println("配置--"+configattibute+"价格--"+goodsrice+"库存--"+stock);
                  //存数据库  配置表 t_commodity_allocation
                  int b = goodsService_chenbin.add_com_all(good.get(0).getId(),configattibute,goodsrice,stock);


            });
            Double pirce = goodsrice;
            System.out.println("价格="+goodsrice);
            goodsService_chenbin.add_com_price(pirce,good.get(0).getId());

            if (n>0)
            {
                  return mall_data.setcode(505).setdata("no");
            }
            return mall_data.setdata("添加成功");
      }



}
